﻿using CIS.Client.Common.HttpRequestHelper;
using CIS.Client.Modules.Operation.Models;
using CIS.Common.Dtos.ClientDtos;
using Newtonsoft.Json;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using static CIS.Client.Modules.Operation.Models.PPIDModel;

namespace CIS.Client.Modules.Operation.ViewModels
{
    public class PPIDViewModel : BindableBase
    {
        private readonly IRegionManager _regionManager;
        private readonly IDialogService _dialogService;
        private readonly HttpRequestClient _httpRequest;

        public PPIDViewModel(IRegionManager regionManager,IDialogService dialogService, HttpRequestClient httpRequestClient)
        {
            _regionManager = regionManager;
            _dialogService = dialogService;
            _httpRequest = httpRequestClient;
            Init();
        }

        #region 属性
        private List<ProceeProgramClientDto> processPrograms;

        public List<ProceeProgramClientDto> ProcessPrograms
        {
            get { return processPrograms; }
            set 
            { 
                processPrograms = value;
                RaisePropertyChanged(nameof(ProcessPrograms)); 
            }
        }
        #endregion

        #region 命令
        private DelegateCommand addPPIDCommand;
        /// <summary>
        /// 界面 Add 按钮命令
        /// </summary>
        public DelegateCommand AddPPIDCommand
        {
            get
            {
                return addPPIDCommand ?? new DelegateCommand(AddPPIDFunction);
            }
        }

        private DelegateCommand deletePPIDCommand;
        /// <summary>
        /// 界面 Delete 按钮命令
        /// </summary>
        public DelegateCommand DeletePPIDCommand
        {
            get
            {
                return deletePPIDCommand ?? new DelegateCommand(DeletePPIDFunction);
            }
        }


        #endregion

        #region 方法
        /// <summary>
        /// 初始化
        /// </summary>
        private async void Init()
        {
            //调用API
            HttpRequest request = new HttpRequest();
            request.Route = "ProcessProgram/GetProcessPrograms";
            RequestResult result = await _httpRequest.SendGetAsync(request);
            ProcessPrograms = JsonConvert.DeserializeObject<List<ProceeProgramClientDto>>(Convert.ToString(result.Data));
            if(ProcessPrograms != null)
            {
                foreach (var item in ProcessPrograms)
                {
                    item.GetRecipeNo();
                }
            }           
        }

        /// <summary>
        /// Add 按钮命令执行逻辑
        /// </summary>
        private void AddPPIDFunction()
        {
            _dialogService.ShowDialog("PPIDAddView", (result) =>
            {
                var para = result.Parameters.GetValue<string>("Message");
                if(result.Result == ButtonResult.OK)
                {
                    MessageBox.Show("Operation Is Success");
                    Init();
                }
                else if(result.Result == ButtonResult.No)
                    MessageBox.Show(para);                
            });
        }

        /// <summary>
        /// Delete 按钮命令执行逻辑
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private async void DeletePPIDFunction()
        {
            if(MessageBox.Show("确定删除此PPID", null, MessageBoxButton.OKCancel)== MessageBoxResult.OK)
            {
                //调用API
                HttpRequest request = new HttpRequest();
                request.Route = "ProcessProgram/DeleteProcessProgram";
                RequestResult result = await _httpRequest.SendPostAsync(request);
                if(result.IsSuccess)
                {
                    MessageBox.Show("删除成功");
                }
            }      
        }
        #endregion



        #region ContentControl 相关属性
        /// <summary>
        /// TabItem 显示名称
        /// </summary>
        public string? PageTitle { get; set; } = "PPID设置";

        /// <summary>
        /// 导航属性
        /// </summary>
        public string? NavUri { get; set; } = "PPIDView";

        /// <summary>
        /// 是否可关闭
        /// </summary>
        public bool IsCanClose { get; set; } = true;

        /// <summary>
        /// TabControl 关闭按钮命令
        /// </summary>
        public DelegateCommand<string>? CloseCommand
        {
            get
            {
                return new DelegateCommand<string>(CloseFunction);
            }
        }

        /// <summary>
        /// TabControl 关闭按钮执行操作
        /// </summary>
        /// <param name="navUri"></param>
        private void CloseFunction(string navUri)
        {
            var removeView = _regionManager.Regions["MainRegion"].Views.FirstOrDefault(v => v.GetType().Name == navUri);
            if (removeView != null)
            {
                _regionManager.Regions["MainRegion"].Remove(removeView);
            }
        }
        #endregion
    }
}
